﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibraryManagementSystem
{
    class Library
    {
        private List<BookClass> _bookList = new List<BookClass>();

        public List<BookClass> BookList
        {
            get { return _bookList; }
            private set { _bookList = value; }
        }

        //▪ 添加新图书（包含书名、作者、库存数量等）+
        public string Add(string BookName, string BookAuthor, int BookAmount)
        {
            if (BookName == "" && BookAuthor == "" ) { return "书名和作者都为空，未能加入库存"; }
            int IsInList = FindBook(BookName, BookAuthor);
            if (IsInList != -1)
            {
                BookList[IsInList].BookAmount += BookAmount;
                return "已存在同种书,已加入库存";
            }
            else
            {
                BookList.Add(new BookClass(BookName, BookAuthor, BookAmount, BookList.Count));
                return "没有同种书,已加入库存";
            }
        }

        //展示库存数量 +
        public int GetBookCount()
        {
            return BookList.Count;
        }

        //▪ 删除现有图书。 +
        public String DeleteBook(string BookName, string BookAuthor)
        {
            if (BookList.Count <= 0) {  return "图书馆的藏书数为零，没有任何书本"; }
            int IsinList = FindBook(BookName, BookAuthor);
            if (IsinList > -1)
            {
                BookList.RemoveAt(IsinList);
                return $"已从书库中移除书籍：{BookName} 作者：{BookAuthor}" ;
            }
            else
            {
                return "图书馆内查无此书";
            }
        }
        //展示所有库存信息+
        public void ShowAllBookDataToConsole()
        {
            if (BookList.Count <= 0) { Console.WriteLine("图书馆的藏书数为零，没有任何书本"); return; }
            foreach (BookClass i in BookList)
            {
                Console.WriteLine($"书本名：{i.BookName}  书本作者;{i.BookAuthor}  书本现存数量：{i.BookAmount}");
            }
            int AllBookAmount = BookList.Sum(BookClass => BookClass.BookAmount);
            Console.WriteLine("库存书籍类总数：" + BookList.Count + "  全馆书册数为：" + AllBookAmount);
            Console.WriteLine("以上是本馆所有的书本");
        }

        //返回书本的信息
        public BookClass GetBookDataByIndex(int index)
        {
            if (BookList.Count <= 0 || index < 0 || index >= BookList.Count) {  return null; }
            BookClass RTBK = BookList[index];
            return RTBK;

        }
        public void SetBookAuthorByIndex(int index,int setValue) 
        {
            BookList[index].BookAmount += setValue;
        }

        //查询图书信息（支持按书名、作者查询）  选择模式 。//返回索引+
        public List<BookClass> FindBook(string FindTypeValue, FindType findType)
        {
            List<BookClass> RTint = new List<BookClass>();
            switch (findType) {
                case FindType.Author :
                    foreach ( BookClass i in BookList ) 
                    {
                        if ( i.BookAuthor == FindTypeValue ) 
                        {
                            RTint.Add(i);
                        }
                    }
                    break;
                case FindType.BookName:
                    foreach ( BookClass i in BookList ) 
                    {
                        if (i.BookName == FindTypeValue) 
                        {
                            RTint.Add(i);
                        }
                    }
                    break;
            }
            return RTint;

        }

        //查询图书信息（支持按书名、作者查询)+
        public int FindBook(string NeedFindBookName,string NeedFindBookAuthor) 
        {
           return BookList.FindIndex(BookClass => BookClass.BookName == NeedFindBookName && BookClass.BookAuthor == NeedFindBookAuthor  );
        }

        
        

        //◦ 借阅系统：
        //▪ 支持用户借阅图书。 
        //▪ 支持用户归还图书。
        //▪ 检查图书库存，防止库存不足时进行借阅操作。

    }
    public enum FindType
    {
        BookName,
        Author
    }

}
